import { TemplateCriteriaModel } from "../../../../../models/templateCriteria.model";
import { ICriteriaTemplateOutput, CriteriaSummary } from "./criteriaTemplateOutput.interface";
import { SliderOutput, InvolvementCategoryOutput } from "./criteriaOutput.model";

export abstract class BaseTemplateOutput implements ICriteriaTemplateOutput {
    constructor(
        public criteriaLevel2Id: string,
        public criteriaLevel3Id: string,
        public type: number,
        public isNotScreenableCriteriaItems: TemplateCriteriaModel[]) { }

    public abstract getTemplateSummary(): CriteriaSummary[];

    public abstract removeCriteriaFromTemplate(criteriaId: string): boolean;

    public abstract dataToSave();
}

export class UnscreenableModel extends BaseTemplateOutput {
    constructor(
        public name: string,
        public criteriaLevel2Id: string,
        public criteriaLevel3Id: string,
        public type: number,
        public isNotScreenableCriteriaItems: TemplateCriteriaModel[],
        public claimSymbol: string = undefined) {
        super(criteriaLevel2Id, criteriaLevel3Id, type, isNotScreenableCriteriaItems);
    }

    getTemplateSummary(): CriteriaSummary[] {
        let ret: CriteriaSummary[] = [];

        this.isNotScreenableCriteriaItems.forEach(nonscreenable => {
            ret.push(new CriteriaSummary(nonscreenable.id, nonscreenable.criteriaLevel2, nonscreenable.name));
        });
        return ret;
    }

    removeCriteriaFromTemplate(criteriaId: string): boolean {
        let index = this.isNotScreenableCriteriaItems.findIndex(x => x.id == criteriaId);
        if (index > -1)
            this.isNotScreenableCriteriaItems.splice(index, 1);

        return this.isNotScreenableCriteriaItems.length == 0;
    }

    dataToSave() {
        throw new Error("Method not implemented.");
    }
}

export class SliderModel extends BaseTemplateOutput {
    constructor(
        public criteriaLevel2Id: string,
        public criteriaLevel3Id: string,
        public type: number,
        public isScreenableCriteriaItems: SliderOutput[],
        public isNotScreenableCriteriaItems: TemplateCriteriaModel[]) {
        super(criteriaLevel2Id, criteriaLevel3Id, type, isNotScreenableCriteriaItems);
    }

    getTemplateSummary(): CriteriaSummary[] {
        let ret: CriteriaSummary[] = [];
        this.isScreenableCriteriaItems.forEach(screenable => {
            ret.push(new CriteriaSummary(screenable.id, this.criteriaLevel2Id, screenable.name));
        });

        this.isNotScreenableCriteriaItems.forEach(nonscreenable => {
            ret.push(new CriteriaSummary(nonscreenable.id, this.criteriaLevel2Id, nonscreenable.name));
        });
        return ret;
    }

    removeCriteriaFromTemplate(criteriaId: string): boolean {
        let index = this.isScreenableCriteriaItems.findIndex(x => x.id == criteriaId);
        if (index > -1)
            this.isScreenableCriteriaItems.splice(index, 1);
        else {
            index = this.isNotScreenableCriteriaItems.findIndex(x => x.id == criteriaId);
            if (index > -1)
                this.isNotScreenableCriteriaItems.splice(index, 1);
        }

        return this.isScreenableCriteriaItems.length == 0 && this.isNotScreenableCriteriaItems.length == 0;
    }

    dataToSave(): any {
        return { isNotScreenableCriteriaItems: this.isNotScreenableCriteriaItems, isScreenableCriteriaItems: this.isScreenableCriteriaItems };
    }
}

export class HistogramModel extends BaseTemplateOutput {
    constructor(public criteriaLevel2Id: string,
        public criteriaLevel3Id: string,
        public id: string,
        public name: string,
        public type: number,
        public isNotScreenableCriteriaItems: TemplateCriteriaModel[],
        public minValue: number,
        public maxValue: number) {
        super(criteriaLevel2Id, criteriaLevel3Id, type, isNotScreenableCriteriaItems);
    }

    getTemplateSummary(): CriteriaSummary[] {
        let ret: CriteriaSummary[] = [];

        ret.push(new CriteriaSummary(this.criteriaLevel3Id, this.criteriaLevel2Id, this.name));

        this.isNotScreenableCriteriaItems.forEach(nonscreenable => {
            ret.push(new CriteriaSummary(nonscreenable.id, nonscreenable.criteriaLevel2, nonscreenable.name));
        });
        return ret;
    }

    removeCriteriaFromTemplate(criteriaId: string): boolean {
        //do we need to remove the histogram interval?        
        let index = this.isNotScreenableCriteriaItems.findIndex(x => x.id == criteriaId);
        if (index > -1)
            this.isNotScreenableCriteriaItems.splice(index, 1);

        return this.isNotScreenableCriteriaItems.length == 0;
    }

    dataToSave() {
        return { id: this.id, isNotScreenableCriteriaItems: this.isNotScreenableCriteriaItems, maxValue: this.maxValue, minValue: this.minValue };
    }

}

export class MultiSelectModel extends BaseTemplateOutput {
    constructor(public criteriaLevel2Id: string,
        public criteriaLevel3Id: string,
        public id: string,
        public name: string,
        public type: number,
        public isNotScreenableCriteriaItems: TemplateCriteriaModel[],
        public isScreenableCriteriaItems: any[]) {
        super(criteriaLevel2Id, criteriaLevel3Id, type, isNotScreenableCriteriaItems);
    }

    getTemplateSummary(): CriteriaSummary[] {
        let ret: CriteriaSummary[] = [];

        ret.push(new CriteriaSummary(this.criteriaLevel3Id, this.criteriaLevel2Id, this.name));

        this.isNotScreenableCriteriaItems.forEach(nonscreenable => {
            ret.push(new CriteriaSummary(nonscreenable.id, nonscreenable.criteriaLevel2, nonscreenable.name));
        });
        return ret;
    }

    removeCriteriaFromTemplate(criteriaId: string): boolean {
        let index = this.isScreenableCriteriaItems.findIndex(x => x.criteriaLevel3Id == criteriaId);
        if (index > -1)
            this.isScreenableCriteriaItems.splice(index, 1);
        else {
            index = this.isNotScreenableCriteriaItems.findIndex(x => x.id == criteriaId);
            if (index > -1)
                this.isNotScreenableCriteriaItems.splice(index, 1);
        }

        return this.isScreenableCriteriaItems.length == 0 && this.isNotScreenableCriteriaItems.length == 0;
    }

    dataToSave() {
        return { id: this.id, isNotScreenableCriteriaItems: this.isNotScreenableCriteriaItems, isScreenableCriteriaItems: this.isScreenableCriteriaItems };
    }
}

export class InvolvementModel extends BaseTemplateOutput {
    constructor(public criteriaLevel2Id: string,
        public criteriaLevel3Id: string,
        public id: string,
        public name: string,
        public type: number,
        public filterGroupId: string,
        public isNotScreenableCriteriaItems: TemplateCriteriaModel[],
        public selectedCategories: InvolvementCategoryOutput[],
        public claimSymbol: string,
        public product: string) {
        super(criteriaLevel2Id, criteriaLevel3Id, type, isNotScreenableCriteriaItems);
    }

    getTemplateSummary(): CriteriaSummary[] {
        let ret: CriteriaSummary[] = [];
        this.selectedCategories.forEach(category => {
            var categoryText = "";

            if (category.isSignificantOwnership)
                categoryText = category.categoryOfInvolvement + ': ' + 'Significant ownership';
            if(category.isInvolvement)
                categoryText = category.categoryOfInvolvement + ': ' + 'Not involved';
            if (category.revenueRange)
                categoryText = category.categoryOfInvolvement + ': ' + (this.product == 'PI' ? category.start + '% and above is restricted' : "Minimum revenue percent is set to: " + category.start + "%" )

            ret.push(
                new CriteriaSummary(
                    category.id,
                    this.criteriaLevel2Id,
                    categoryText
                )
            );
        });

        this.isNotScreenableCriteriaItems.forEach(nonscreenable => {
            ret.push(new CriteriaSummary(nonscreenable.id, nonscreenable.criteriaLevel2, nonscreenable.name));
        });
        return ret;
    }

    removeCriteriaFromTemplate(criteriaId: string): boolean {
        let index = this.selectedCategories.findIndex(x => x.id == criteriaId);
        if (index > -1)
            this.selectedCategories.splice(index, 1);
        else {
            index = this.isNotScreenableCriteriaItems.findIndex(x => x.id == criteriaId);
            if (index > -1)
                this.isNotScreenableCriteriaItems.splice(index, 1);
        }

        return this.selectedCategories.length == 0 && this.isNotScreenableCriteriaItems.length == 0;
    }

    dataToSave() {
        return {
            id: this.id,
            name: this.name,
            claimSymbol: this.claimSymbol,
            criteriaLevel2Id: this.criteriaLevel2Id,
            filterGroupId: this.filterGroupId,
            type: this.type,
            isNotScreenableCriteriaItems: this.isNotScreenableCriteriaItems,
            selectedCategories: this.selectedCategories
        };
    }

}

